<?php

namespace app\index\controller;

use app\api\model\AccessTokenModel;
use app\api\model\Token;
use app\common\controller\Api;
use app\common\model\DouyinModel;
use think\Controller;
use think\Db;
use think\Exception;
use think\Request;
use util\Http;

class Index extends Controller
{
    const URL_TOKEN = 'https://open-api.ixigua.com/oauth/access_token/';

    const DOUYIN_URL = 'https://open.douyin.com';


    protected $noNeedLogin = ['*'];

    public function index(Request $request)
    {
        return $this->fetch();
    }

    /**
     * @title 登陆
     */
    public function index1(Request $request)
    {

        $params = $request->param();

        $res = (new DouyinModel())->dyLogin($params);

        print_r($res);die;
    }


    public function getUserInfo($access_token, $openid)
    {

        $url = self::DOUYIN_URL . '/oauth/userinfo/';

        $params = array(
            'access_token' => $access_token,
            'open_id' => $openid
        );


        $result = $this->curl_post($url, $params);
        return $result;
    }


    function curl_post($url, $postdata)
    {

        $header = array(
            'Accept: application/json',
        );

        //初始化
        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // 超时设置
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);

        // 超时设置，以毫秒为单位
        // curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);

        // 设置请求头
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
        //执行命令
        $data = curl_exec($curl);

        // 显示错误信息
        if (curl_error($curl)) {
            print "Error: " . curl_error($curl);
        } else {
            // 打印返回的内容
            curl_close($curl);

            return json_decode($data, true);
        }
    }
}
